package com.link.base.base.user.controller;

import com.link.base.base.user.model.UserConfig;
import com.link.base.base.user.model.UserTableConfig;
import com.link.base.base.user.service.UserConfigService;
import com.link.core.cllog.LCLogger;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户自定义配置操作相关接口
 *
 * @author 韦胜健
 * @date 2019/2/18 11:54
 */
@RestController
@RequestMapping("/userConfig")
public class UserConfigController {

    @Resource
    private UserConfigService userConfigService;

    /**
     * 获取当前用户配置信息对象
     *
     * @author 韦胜健
     * @date 2019/4/17 14:12
     */
    @RequestMapping("getUserConfig")
    public Map<String, Object> getUserConfig() {
        Map<String, Object> ret = new HashMap<>();
        try {
            ret.put("success", true);
            ret.put("result", userConfigService.getUserConfig());
        } catch (Exception e) {
            LCLogger.error().withMessageKey("userConfig").withMessageKey("获取用户自定义配置信息失败！" + e.getMessage());
            ret.put("success", false);
            ret.put("result", e.getMessage());
        }
        return ret;
    }

    /**
     * 保存用户配置信息对象
     *
     * @author 韦胜健
     * @date 2019/4/17 14:13
     */
    @RequestMapping("saveUserConfig")
    public Map<String, Object> saveUserConfig(@RequestBody UserConfig userConfig) {
        Map<String, Object> ret = new HashMap<>();
        try {
            userConfigService.saveUserConfig(userConfig);
            ret.put("success", true);
            ret.put("result", "保存成功！");
        } catch (Exception e) {
            LCLogger.error().withMessageKey("userConfig").withMessageKey("保存用户自定义配置信息失败！" + e.getMessage());
            ret.put("success", false);
            ret.put("result", e.getMessage());
        }
        return ret;
    }

    /**
     * 删除用户配置信息对象
     *
     * @author 韦胜健
     * @date 2019/4/17 14:13
     */
    @RequestMapping("deleteUserConfig")
    public Map<String, Object> deleteUserConfig() {
        Map<String, Object> ret = new HashMap<>();
        try {
            userConfigService.deleteUserConfig();
            ret.put("success", true);
            ret.put("result", "删除成功！");
        } catch (Exception e) {
            LCLogger.error().withMessageKey("userConfig").withMessageKey("删除用户自定义配置信息失败！" + e.getMessage());
            ret.put("success", false);
            ret.put("result", e.getMessage());
        }
        return ret;
    }

    /**
     * 示例接口：保存用户主题色
     *
     * @author 韦胜健
     * @date 2019/4/17 14:13
     */
    @RequestMapping("saveThemeColor")
    public Map<String, Object> saveThemeColor(@RequestBody UserConfig userConfig) {
        Map<String, Object> ret = new HashMap<>();
        try {
            userConfigService.saveThemeColor(userConfig.getThemeColor());
            ret.put("success", true);
            ret.put("result", "保存成功！");
        } catch (Exception e) {
            LCLogger.error().withMessageKey("userConfig").withMessageKey("保存用户个性化主题色失败！" + e.getMessage());
            ret.put("success", false);
            ret.put("result", e.getMessage());
        }
        return ret;
    }

    /**
     * 保存表格配置信息
     *
     * @author 韦胜健
     * @date 2019/4/17 14:15
     */
    @RequestMapping("saveTableConfig")
    public Map<String, Object> saveTableConfig(@RequestBody UserTableConfig userTableConfig) {
        Map<String, Object> ret = new HashMap<>();
        try {
            userConfigService.saveTableConfig(userTableConfig);
            ret.put("success", true);
            ret.put("result", userTableConfig);
        } catch (Exception e) {
            LCLogger.error().withMessageKey("saveTableConfig").withMessageKey("保存表格配置信息失败！" + e.getMessage());
            ret.put("success", false);
            ret.put("result", e.getMessage());
        }
        return ret;
    }

    /**
     * 删除表格配置信息
     *
     * @author 韦胜健
     * @date 2019/4/17 14:15
     */
    @RequestMapping("deleteTableConfig")
    public Map<String, Object> deleteTableConfig(@RequestBody UserTableConfig userTableConfig) {
        Map<String, Object> ret = new HashMap<>();
        try {
            userConfigService.deleteTableConfig(userTableConfig);
            ret.put("success", true);
            ret.put("result", userTableConfig);
        } catch (Exception e) {
            LCLogger.error().withMessageKey("deleteTableConfig").withMessageKey("删除表格配置信息失败！" + e.getMessage());
            ret.put("success", false);
            ret.put("result", e.getMessage());
        }
        return ret;
    }


}
